﻿
Partial Class TranscriptWithPaging
    Inherits CPage

    Private Enum ELang
        English
        ChineseSimplified
        ChineseTraditional
    End Enum

#Region "Querystring"
    Public ReadOnly Property ProductId() As Integer
        Get
            If Page.RouteData.Values.ContainsKey("productId") Then
                Return Page.RouteData.Values("productId")
            Else
                ProductId = CWeb.RequestInt("productId")
                Dim p As CProduct = CProduct.Cache.GetById(ProductId)
                If IsNothing(p) Then Response.RedirectPermanent(CSitemap.Transcripts)
                Response.RedirectPermanent(CSitemap.TranscriptWithPaging(p))
            End If
        End Get
    End Property
    Public ReadOnly Property PageNumber() As Integer
        Get
            If Page.RouteData.Values.ContainsKey("p") Then
                Return Page.RouteData.Values("p")
            Else
                PageNumber = CWeb.RequestInt("p", 1)
            End If
        End Get
    End Property
    Public ReadOnly Property ProductName As String
        Get
            If Page.RouteData.Values.ContainsKey("productName") Then
                Return Page.RouteData.Values("productName")
            Else
                Return CConfig.SafeFriendlyName(Product.ProductName)
            End If
        End Get
    End Property

    'From path
    Public ReadOnly Property IsChineseSimplified As Boolean
        Get
            Return Request.RawUrl.Contains("/chinese_simplified/")
        End Get
    End Property
    Public ReadOnly Property IsChineseTraditional As Boolean
        Get
            Return Request.RawUrl.Contains("/chinese_traditional/")
        End Get
    End Property
    Private ReadOnly Property Lang As ELang
        Get
            If IsChineseSimplified Then Return ELang.ChineseSimplified
            If IsChineseTraditional Then Return ELang.ChineseTraditional
            Return ELang.English
        End Get
    End Property
#End Region

#Region "Data"
    Public ReadOnly Property [Product]() As CProduct
        Get
            Return CProduct.Cache.GetById(ProductId)
        End Get
    End Property
#End Region

    Protected Overrides Sub PageInit()

        lnkSearch.NavigateUrl = CSitemap.Transcripts
        If IsChineseSimplified OrElse IsChineseTraditional Then
            For Each i As Control In Me.Header.Controls
                If TypeOf i Is HtmlMeta Then
                    Dim meta As HtmlMeta = i
                    If meta.HttpEquiv.ToLower = "content-language" Then
                        If IsChineseSimplified Then
                            meta.Content = "zh_HANS"
                        Else
                            meta.Content = "zh_HANT"
                        End If
                    End If
                End If
            Next
            lnkSearch.Text = "搜索"
            lnkSearch.NavigateUrl = CSitemap.Chinese
            lnkBackToSet.Text = "集"
            lnkBackToSermon.Text = "讲道"

            lnkHtml2.ToolTip = lnkHtml2.Text
            lnkHtml2.Text = "查看整个文件 &raquo;"

            lnkPrev.Text = lnkPrev.Text.Replace("Prev Page", "上一页")
            lnkNext.Text = lnkNext.Text.Replace("Next Page", "下一页")
        End If

        ShowLinks()
        lnkHtml2.NavigateUrl = CSitemap.Transcript(Product)
        With Product

            If Not .HasTranscript AndAlso Not .IsSet Then Response.Redirect(CSitemap.Decide(Product))


            If IsChineseSimplified Then
                litTitle.Text = .ProductNameChineseSimplified
            ElseIf IsChineseTraditional Then
                litTitle.Text = .ProductNameChineseTraditionalOrSimplified
            Else
                litTitle.Text = .ProductName
            End If
            litTitle.ToolTip = .ProductName

            litSpeaker.Text = .SpeakerNames
            lnkBackToSermon.NavigateUrl = CSitemap.Decide(Product)
            lnkBackToSermon2.NavigateUrl = CSitemap.Decide(Product)
            lnkBackToSet.NavigateUrl = CSitemap.Decide(Product)
            lnkContents.NavigateUrl = CSitemap.TranscriptWithPaging(Product)

            If IsChineseSimplified Then
                divLike.Attributes.Add("data-href", CSitemap.TranscriptChineseSimplified(Product))
                litSubTitle.Text = String.Concat("共", .ChaptersChineseSimp.Count, "页面", PageNumber)
            ElseIf IsChineseTraditional Then
                divLike.Attributes.Add("data-href", CSitemap.TranscriptChineseTraditional(Product))
                litSubTitle.Text = String.Concat("共", .ChaptersChineseTrad.Count, "頁面", PageNumber)
            Else
                divLike.Attributes.Add("data-href", CSitemap.TranscriptWithPaging_(Product))
                litSubTitle.Text = String.Concat("Page ", PageNumber, " of ", .Chapters.Count)
            End If
            lnkDonate.NavigateUrl = CSitemap.External_Donations


            If .HasParent Then
                'lnkBack.Text = "&laquo;Collection"
                lnkContents.CssClass = "menu"
                lnkContents.NavigateUrl = CSitemap.TranscriptWithPaging(.BelongsTo)
                With .BelongsTo
                    'litTitle.Text = .ProductName
                    litSpeaker.Text = .SpeakerNames
                    lnkBackToSet.NavigateUrl = CSitemap.Decide(Product.BelongsTo)
                    lnkBackToSermon.ToolTip = .ProductName
                    lnkBackToSermon2.ToolTip = .ProductName

                    AddLinks(.Children)
                End With

                If PageNumber = 100 Then
                    Me.Title = String.Concat(.ProductName, " (", .BelongsToName, "): Summary Notes")
                    litSubTitle.Text = "Summary Notes"
                    lnkBackToSermon2.Visible = Not String.IsNullOrEmpty(.ProductNotesAsPlainText)
                ElseIf PageNumber > 1 Then
                    Me.Title = String.Concat(.ProductName, " (Page ", PageNumber, " of ", Product.Chapters.Count, "): Audio Transcripts")
                Else
                    Me.Title = String.Concat(.ProductName, " (", .BelongsToName, "): Audio Transcripts")
                End If

                ShowPage()

                If IsChineseSimplified Then
                    AddPagesSimplified()
                ElseIf IsChineseTraditional Then
                    AddPagesTraditional()
                Else
                    AddPages()
                End If
            Else

                lnkBackToSermon.ToolTip &= .ProductName
                lnkBackToSermon2.ToolTip &= .ProductName

                If PageNumber = 100 Then
                    Me.Title = String.Concat(.ProductName, ": Summary Notes")
                ElseIf PageNumber > 1 Then
                    Me.Title = String.Concat(.ProductName, " (Page ", PageNumber, " of ", Product.Chapters.Count, "): Audio Transcript")
                Else
                    Me.Title = String.Concat(.ProductName, ": Audio Transcript")
                End If

                If .IsSet Then
                    'nkBack.Text = "&laquo;Collection"
                    AddLinks(.Children)

                    ShowContents(Product)

                    lnkBackToSermon.Visible = False
                Else
                    lnkBackToSet.Visible = False

                    lnkContents.Text = "Transcript"
                    If 100 = PageNumber Then
                        litSubTitle.Text = "Sermon Notes"
                    End If

                    ShowPage()
                    AddPages()
                End If
            End If




            If IsChineseSimplified Then
                Page.MetaKeywords = .ProductKeywordsChineseSimplifiedOrParent
                Page.MetaDescription = .ProductDescriptionChineseSimplifiedOrParent
                If Page.MetaDescription.Length = 0 Then Page.MetaDescription = CUtilities.Truncate(.Speaker.SpeakerAbout, 400)
                Page.MetaDescription = "Transcript: " & Page.MetaDescription
            ElseIf IsChineseTraditional Then
                Page.MetaKeywords = .ProductKeywordsChineseTraditionaldOrParent
                Page.MetaDescription = .ProductDescriptionChineseTraditionalOrParent
                If Page.MetaDescription.Length = 0 Then Page.MetaDescription = CUtilities.Truncate(.Speaker.SpeakerAbout, 400)
                Page.MetaDescription = "Transcript: " & Page.MetaDescription
            Else
                Page.MetaKeywords = .ProductKeywordsOrParent
                Page.MetaDescription = .ProductDescriptionOrParent
                If Page.MetaDescription.Length = 0 Then Page.MetaDescription = CUtilities.Truncate(.Speaker.SpeakerAbout, 400)
                Page.MetaDescription = "Transcript: " & Page.MetaDescription
            End If

            If Not String.IsNullOrEmpty(.ProductImage) Then
                imgFb.Attributes("href") = "http://mikeconnellministries.com" & CSitemap.ProductThumb(Product).Substring(1)
            ElseIf Not String.IsNullOrEmpty(.Speaker.SpeakerImage) Then
                imgFb.Attributes("href") = "http://mikeconnellministries.com" & CSitemap.SpeakerUploads & .Speaker.SpeakerImage.Substring(1)
            End If

            img.AlternateText = .ProductName
            img.ImageUrl = CSitemap.ProductImage(Product)
            If CSitemap.ProductUploads(True) = img.ImageUrl Then
                img.ImageUrl = Product.Speaker.RandomPhotoUrl(CSitemap.SpeakerUploads(True) & Product.Speaker.SpeakerImage)
            End If

            If PageNumber = 1 Then
                If IsChineseSimplified Then
                    lblIntro.Text = .DescriptionChineseSimp_Br
                ElseIf IsChineseTraditional Then
                    lblIntro.Text = .DescriptionChineseTrad_Br
                Else
                    lblIntro.Text = .Description_Br
                End If
                If lblIntro.Text.Length = 0 Then lblIntro.Text = .Description_Br

                If .IsSet Then
                    litSubTitle.Text = .ProductName
                    lnkBackToSermon2.Visible = False
                    lnkBackToSermon3.Visible = True
                    lnkBackToSermon3.NavigateUrl = lnkBackToSermon2.NavigateUrl
                    img2.ImageUrl = img.ImageUrl
                    lblIntro.Visible = False
                    ctrlLhs.Visible = False
                End If
            Else
                lblIntro.Visible = False
                lnkBackToSermon2.Visible = False
            End If
        End With

    End Sub

#Region "Display"


    Private Sub ShowPage()
        If IsChineseSimplified Then
            If PageNumber = 100 Then
                If Not IsNothing(Product.ProductNotesAsPlainText) Then
                    litDisplayPage.Text = CKindle.EncodeAndReplace(Product.ProductNotesAsPlainText)
                End If
            ElseIf PageNumber > Product.ChaptersChineseSimp.Count Then
                Response.RedirectPermanent(CSitemap.TranscriptChineseSimplified(Product))
            Else
                litDisplayPage.Text = CKindle.EncodeAndReplace(Product.ChaptersChineseSimp(PageNumber - 1))
            End If
        ElseIf IsChineseTraditional Then
            If PageNumber = 100 Then
                If Not IsNothing(Product.ProductNotesAsPlainText) Then
                    litDisplayPage.Text = CKindle.EncodeAndReplace(Product.ProductNotesAsPlainText)
                    'litDisplayPage.Text = String.Concat("<div class='vert'>", litDisplayPage.Text, "</div>")
                End If
            ElseIf PageNumber > Product.ChaptersChineseTrad.Count Then
                Response.RedirectPermanent(CSitemap.TranscriptChineseTraditional(Product))
            Else
                litDisplayPage.Text = CKindle.EncodeAndReplace(Product.ChaptersChineseTrad(PageNumber - 1))
                'litDisplayPage.Text = String.Concat("<div class='vert'>", litDisplayPage.Text, "</div>")
            End If
        Else
            If PageNumber = 100 Then
                If Not IsNothing(Product.ProductNotesAsPlainText) Then
                    litDisplayPage.Text = String.Concat("<p class=notes>", CKindle.EncodeAndReplace(Product.ProductNotesAsPlainText), "</p>")
                End If
            ElseIf PageNumber > Product.Chapters.Count Then
                Response.RedirectPermanent(CSitemap.TranscriptWithPaging(Product))
            Else
                litDisplayPage.Text = CKindle.EncodeAndReplace(Product.Chapters(PageNumber - 1))
            End If

        End If
        plhSquish.Visible = False
    End Sub

    Private Sub ShowContents(p As CProduct)
        With New CKindle(EFormat.HTML)
            .IsPagedHtml = True
            litDisplayContents.Text = .Contents_Intro(p.Children.Sermons, True)
        End With
        lnkNext.Visible = False
        lnkPrev.Visible = False
    End Sub
#End Region

#Region "Ebook Nav"
    Public Sub ShowLinks()
        Dim p As CProduct = Product
        If p.HasParent Then p = p.BelongsTo
        ShowLinks1(plhLhs, p)
        'ShowLinks1(plhRhs, p)

        ShowLinks2(plhLhs2, p)
        'ShowLinks2(plhRhs2, p)

        ShowLinksChineseSimplified(plhLhs3, p)
        ShowLinksChineseTraditional(plhLhs3, p)

        ShowLinksForSpeaker(plhLhs3, p, True, ESpeaker.MikeConnell)
        ShowLinksForSpeaker(plhLhs3, p, True, ESpeaker.ShaneWillard)
        ShowLinksForSpeaker(plhLhs3, p, False, ESpeaker.MikeConnell)
        ShowLinksForSpeaker(plhLhs3, p, False, ESpeaker.ShaneWillard)
        'ShowLinksSets(plhLhs3, p)
        'ShowLinksStandalone(plhLhs3, p)


        Dim download As String = "Download Audio"
        If IsChineseSimplified Then download = "下载音频"
        If IsChineseTraditional Then download = "下載音頻"
        If Not Product.IsSet Then ShowLink(plhLhs4, download, CConfig.Mp3(Product), "Download the Audio") Else plhLhs4.Visible = False
    End Sub
    Public Sub ShowLinks1(plh As Control, p As CProduct)
        If Not String.IsNullOrEmpty(p.ProductAmazonId) Then
            ShowLink(plh, "Kindle", CConfig.External_Amazon(p.ProductAmazonId), "Download from the Amazon/Kindle store")
        End If
        If Not IsNothing(p.CSPaperback) Then
            Tilde(plh)
            ShowLink(plh, "Paperback", CConfig.External_CreateSpace(p.CSPaperback.ProductCSBookId), "Purchase as a Paperback")
        ElseIf p.ProductSmashwordsId > 0 Then
            Tilde(plh)
            ShowLink(plh, "Other", CConfig.External_Amazon(p.ProductAmazonId), "Various formats are available from Smashwords")
        End If

        Dim div As New Panel
        div.Style.Add("margin-top", "5px")
        plh.Controls.Add(div)
        If p.HasVimeoChannel Then
            ShowLink(div, "Vimeo", p.VideoUrl_VimeoChannel, "Vimeo Channel")
        End If
        If p.HasYouTubePlaylist Then
            Tilde(div)
            ShowLink(div, "YouTube", p.VideoUrl_YouTubePlaylist, "Youtube Playlist")
        End If
    End Sub
    Public Sub ShowLinks2(plh As Control, p As CProduct)

        If p.Files.HasEpub Then
            With p.Files.FirstEpub
                ShowLink(plh, "Epub", .FileName)
            End With
        End If
        If p.Files.HasMobi Then
            Tilde(plh)
            With p.Files.FirstMobi
                ShowLink(plh, "Mobi", .FileName)
            End With
        End If
        If p.Files.HasEpub OrElse p.Files.HasMobi Then
            Tilde(plh)
            ShowLink(plh, "Html", CSitemap.Transcript(p.ProductId), "View as a single Page")
        End If

    End Sub
    Public Sub ShowLinksChineseSimplified(plh As Control, p As CProduct)
        Dim name As String = "中国 - 简体和繁体"
        Dim ttip As String = "中国 - 简体和繁体 Chinese (Simplified and Traditional)"
        If IsChineseSimplified Then
            name = "中国 - 简体"
            ttip = "中国 - 简体 Chinese (Simplified)"
        End If
        With ShowLink(plh, CUtilities.NameAndCount(name, CProduct.Cache.Active.HavingTranscriptsChineseSimp), CSitemap.Chinese, ttip)
            .Target = String.Empty
            .Font.Size = New FontUnit("12px")
            .ForeColor = Drawing.Color.Black
            .Font.Bold = True
        End With

        For Each i As CProduct In CProduct.Cache.Active.HavingTranscriptsChineseSimp
            AddLink(plh, i, Me.ProductId, ELang.ChineseSimplified)
        Next
    End Sub
    Public Sub ShowLinksChineseTraditional(plh As Control, p As CProduct)
        If Not IsChineseSimplified AndAlso Not IsChineseTraditional Then Exit Sub

        Dim lit As New HtmlGenericControl("br")
        plh.Controls.Add(lit)

        With ShowLink(plh, CUtilities.NameAndCount("中國 - 傳統", CProduct.Cache.Active.HavingTranscriptsChineseTrad), CSitemap.Chinese, "中國 - 傳統 Chinese (Traditional)")
            .Target = String.Empty
            .Font.Size = New FontUnit("12px")
            .ForeColor = Drawing.Color.Black
            .Font.Bold = True
        End With

        For Each i As CProduct In CProduct.Cache.Active.HavingTranscriptsChineseTrad
            AddLink(plh, i, Me.ProductId, ELang.ChineseTraditional)
        Next
    End Sub
    Public Sub ShowLinksSets(plh As Control, p As CProduct)
        Dim lit As New HtmlGenericControl("br")
        plh.Controls.Add(lit)

        With ShowLink(plh, CUtilities.NameAndCount("Collections", CProduct.Cache.Active.ConferenceSets.HavingEpub), CSitemap.ConferenceSets, "View all Collections")
            .Target = String.Empty
            .Font.Size = New FontUnit("12px")
            .ForeColor = Drawing.Color.Black
            .Font.Bold = True
        End With
        For Each i As CProduct In CProduct.Cache.Active.ConferenceSets
            If i.Children.HavingTranscripts.Count = 0 Then Continue For
            AddLink(plh, i, p.ProductId, ELang.English)
        Next
    End Sub
    Public Sub ShowLinksForSpeaker(plh As Control, p As CProduct, showSets As Boolean?, speakerId As ESpeaker)
        ShowLinksForSpeaker(plh, p, showSets, speakerId, CSpeaker.Cache.GetById(speakerId).SpeakerName)
    End Sub
    Public Sub ShowLinksForSpeaker(plh As Control, p As CProduct, showSets As Boolean?, speakerId As ESpeaker, speakerName As String)
        'Dim lit As New HtmlGenericControl("br")
        'plh.Controls.Add(lit)

        Dim speak As CProductList = CProduct.Cache.Active.GetBySpeaker1or2Id(speakerId)
        Dim sets As CProductList = speak.ConferenceSets.HavingChildrenWithTranscripts
        Dim other As CProductList = speak.Sermons.StandAlone.HavingTranscripts

        If sets.Count > 0 AndAlso (Not showSets.HasValue OrElse showSets.Value) Then
            With ShowLink(plh, CUtilities.NameAndCount(speakerName & " Sets", sets), CSitemap.ConferenceSets(Nothing, speakerId), speakerName & " Sermons")
                .Target = String.Empty
                .Font.Size = New FontUnit("12px")
                .ForeColor = Drawing.Color.Black
                .Font.Bold = True
                .Style.Add("margin-top", "10px")
                .Style.Add("display", "block")
            End With
            For Each i As CProduct In sets
                AddLink(plh, i, p.ProductId, ELang.English)
            Next
        End If

        If other.Count > 0 AndAlso (Not showSets.HasValue OrElse Not showSets.Value) Then
            With ShowLink(plh, CUtilities.NameAndCount(speakerName & " Sermons", other), CSitemap.Sermons(Nothing, speakerId), speakerName & " Sermons")
                .Target = String.Empty
                .Font.Size = New FontUnit("12px")
                .ForeColor = Drawing.Color.Black
                .Font.Bold = True
                .Style.Add("margin-top", "10px")
                .Style.Add("display", "block")
            End With
            For Each i As CProduct In other
                AddLink(plh, i, p.ProductId, ELang.English)
            Next
        End If
    End Sub
    Public Sub ShowLinksStandalone(plh As Control, p As CProduct)
        Dim lit As New HtmlGenericControl("br")
        plh.Controls.Add(lit)

        With ShowLink(plh, CUtilities.NameAndCount("Standalone", CProduct.Cache.Active.Sermons.StandAlone.HavingTranscripts), CSitemap.Transcripts, "View all Transcripts")
            .Target = String.Empty
            .Font.Size = New FontUnit("12px")
            .ForeColor = Drawing.Color.Black
            .Font.Bold = True
        End With

        For Each i As CProduct In CProduct.Cache.Active.Sermons.StandAlone.HavingTranscripts
            AddLink(plh, i, p.ProductId, ELang.English)
        Next
    End Sub

    Private Shared Sub ShowLink(plh As Control, name As String, fileName As String)
        ShowLink(plh, name, CConfig.DropboxUrl(fileName), IO.Path.GetFileName(fileName))
    End Sub
    Private Shared Sub Tilde(plh As Control)
        If plh.Controls.Count = 0 Then Exit Sub
        Dim lbl As New Label
        lbl.Text = "~"
        plh.Controls.Add(lbl)
    End Sub
    Private Shared Function ShowLink(plh As Control, name As String, url As String, tooltip As String) As HyperLink
        Dim lnk As New HyperLink
        lnk.Text = name
        lnk.NavigateUrl = url
        lnk.ToolTip = tooltip
        lnk.Target = "_blank"
        plh.Controls.Add(lnk)
        Return lnk
    End Function
#End Region

#Region "Nav within sermon"
    Private Sub AddPages()
        Dim siblings As New CProductList
        If Product.HasParent Then siblings = Product.BelongsTo.Children.Sermons.HavingTranscripts
        Dim index As Integer = siblings.IndexOf(Product)


        If PageNumber = 1 Then
            If index <= 0 Then
                lnkPrev.Style("visibility") = "hidden"
            Else
                lnkPrev.Text = "&laquo; Previous Sermon"
                lnkPrev.NavigateUrl = CSitemap.TranscriptWithPaging(siblings(index - 1))
                lnkPrev.ToolTip = siblings(index - 1).ProductName
            End If
        ElseIf PageNumber = 100 Then
            lnkPrev.NavigateUrl = CSitemap.TranscriptWithPaging(Product, Product.Chapters.Count)
        Else
            lnkPrev.NavigateUrl = CSitemap.TranscriptWithPaging(Product, PageNumber - 1)
        End If


        If PageNumber = Product.Chapters.Count OrElse PageNumber = 100 Then
            If PageNumber <> 100 Then 'Last page is often too small for ads
                'ctrlRhs.Visible = False
                ctrlLhs.Visible = False
            End If
            If Product.HasNotes And PageNumber <> 100 Then
                lnkNext.NavigateUrl = CSitemap.TranscriptWithPaging(Product, 100) 'Link to notes
            ElseIf Product.HasParent AndAlso index + 1 <> siblings.Count Then
                lnkPrev.Text = "Next Sermon &raquo;"
                lnkNext.NavigateUrl = CSitemap.TranscriptWithPaging(siblings(index + 1))
                lnkNext.ToolTip = siblings(index + 1).ProductName
            Else
                lnkNext.Style("visibility") = "hidden"
            End If
        Else
            If PageNumber = 1 Then ctrlLhs.Visible = False
            lnkNext.NavigateUrl = CSitemap.TranscriptWithPaging(Product, PageNumber + 1)
        End If

        For i As Integer = 1 To Product.Chapters.Count
            Dim lnk As New HyperLink
            If i = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = i
            lnk.NavigateUrl = CSitemap.TranscriptWithPaging(Product, i)
            lnk.ToolTip = "Chapter #" & i
            plhPages.Controls.Add(lnk)
        Next

        If Product.HasNotes Then 'Page=100
            Dim lnk As New HyperLink
            If 100 = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = "Notes"
            lnk.NavigateUrl = CSitemap.TranscriptWithPaging(Product, 100)
            lnk.ToolTip = "Summary Notes"
            plhPages.Controls.Add(lnk)
        End If

        Dim lnkWhole As New HyperLink
        lnkWhole.Text = "All &raquo;"
        lnkWhole.NavigateUrl = CSitemap.Transcript(Product)
        lnkWhole.ToolTip = "View the whole transcript, including any others in the collection"
        lnkWhole.Target = "_blank"
        plhPages.Controls.Add(lnkWhole)
    End Sub
    Private Sub AddPagesSimplified()
        Dim siblings As New CProductList
        If Product.HasParent Then siblings = Product.BelongsTo.Children.Sermons.HavingTranscriptsChineseSimp
        Dim index As Integer = siblings.IndexOf(Product)


        If PageNumber = 1 Then
            If index <= 0 Then
                lnkPrev.Style("visibility") = "hidden"
            Else
                lnkPrev.Text = " &laquo; 接着讲道"
                lnkPrev.NavigateUrl = CSitemap.TranscriptChineseSimplified(siblings(index - 1))
                lnkPrev.ToolTip = siblings(index - 1).ProductName
            End If
        ElseIf PageNumber = 100 Then
            lnkPrev.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, Product.ChaptersChineseSimp.Count)
        Else
            lnkPrev.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, PageNumber - 1)
        End If


        If PageNumber = Product.ChaptersChineseSimp.Count OrElse PageNumber = 100 Then
            If PageNumber <> 100 Then 'Last page is often too small for ads
                'ctrlRhs.Visible = False
                ctrlLhs.Visible = False
            End If
            If Product.HasNotes And PageNumber <> 100 Then
                lnkNext.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, 100) 'Link to notes
            ElseIf Product.HasParent AndAlso index + 1 <> siblings.Count Then
                lnkNext.Text = "接着讲道 &raquo;"
                lnkNext.NavigateUrl = CSitemap.TranscriptChineseSimplified(siblings(index + 1))
                lnkNext.ToolTip = siblings(index + 1).ProductName
            Else
                lnkNext.Style("visibility") = "hidden"
            End If
        Else
            If PageNumber = 1 Then ctrlLhs.Visible = False
            lnkNext.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, PageNumber + 1)
        End If

        For i As Integer = 1 To Product.ChaptersChineseSimp.Count
            Dim lnk As New HyperLink
            If i = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = i
            lnk.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, i)
            lnk.ToolTip = "Chapter #" & i
            plhPages.Controls.Add(lnk)
        Next

        If Product.HasNotes Then 'Page=100
            Dim lnk As New HyperLink
            If 100 = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = "笔记"
            lnk.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product, 100)
            lnk.ToolTip = "Summary Notes"
            plhPages.Controls.Add(lnk)
        End If

        Dim lnkWhole As New HyperLink
        lnkWhole.Text = "整个文件 &raquo;"
        lnkWhole.NavigateUrl = CSitemap.Transcript(Product)
        lnkWhole.ToolTip = "View the whole transcript, including any others in the collection"
        lnkWhole.Target = "_blank"
        plhPages.Controls.Add(lnkWhole)
    End Sub
    Private Sub AddPagesTraditional()
        Dim siblings As New CProductList
        If Product.HasParent Then siblings = Product.BelongsTo.Children.Sermons.HavingTranscriptsChineseTrad
        Dim index As Integer = siblings.IndexOf(Product)


        If PageNumber = 1 Then
            If index <= 0 Then
                lnkPrev.Style("visibility") = "hidden"
            Else
                lnkPrev.Text = "&laquo; 接著講道"
                lnkPrev.NavigateUrl = CSitemap.TranscriptChineseTraditional(siblings(index - 1))
                lnkPrev.ToolTip = siblings(index - 1).ProductName
            End If
        ElseIf PageNumber = 100 Then
            lnkPrev.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, Product.ChaptersChineseTrad.Count)
        Else
            lnkPrev.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, PageNumber - 1)
        End If


        If PageNumber = Product.ChaptersChineseTrad.Count OrElse PageNumber = 100 Then
            If PageNumber <> 100 Then 'Last page is often too small for ads
                'ctrlRhs.Visible = False
                ctrlLhs.Visible = False
            End If
            If Product.HasNotes And PageNumber <> 100 Then
                lnkNext.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, 100) 'Link to notes
            ElseIf Product.HasParent AndAlso index + 1 <> siblings.Count Then
                lnkNext.Text = "接著講道 &raquo;"
                lnkNext.NavigateUrl = CSitemap.TranscriptChineseTraditional(siblings(index + 1))
                lnkNext.ToolTip = siblings(index + 1).ProductName
            Else
                lnkNext.Style("visibility") = "hidden"
            End If
        Else
            If PageNumber = 1 Then ctrlLhs.Visible = False
            lnkNext.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, PageNumber + 1)
        End If

        For i As Integer = 1 To Product.ChaptersChineseTrad.Count
            Dim lnk As New HyperLink
            If i = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = i
            lnk.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, i)
            lnk.ToolTip = "Chapter #" & i
            plhPages.Controls.Add(lnk)
        Next

        If Product.HasNotes Then 'Page=100
            Dim lnk As New HyperLink
            If 100 = PageNumber Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
            lnk.Text = "筆記"
            lnk.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product, 100)
            lnk.ToolTip = "Summary Notes"
            plhPages.Controls.Add(lnk)
        End If

        Dim lnkWhole As New HyperLink
        lnkWhole.Text = "整個文件 &raquo;"
        lnkWhole.NavigateUrl = CSitemap.Transcript(Product)
        lnkWhole.ToolTip = "View the whole transcript, including any others in the collection"
        lnkWhole.Target = "_blank"
        plhPages.Controls.Add(lnkWhole)
    End Sub
#End Region

#Region "Nav within set"
    Private Sub AddLinks(kids As CProductList)
        If Lang <> ELang.English Then
            lnkContents.Text = "英语 English"
            lnkContents.NavigateUrl = CSitemap.TranscriptWithPaging(Product)
            lnkContents.Style.Add("margin-right", "20px")
        End If

        If Product.HasTranscriptChineseSimp Then
            Dim lnkSimp As New HyperLink
            lnkSimp.Text = "简体"
            lnkSimp.Style.Add("float", "left")
            lnkSimp.NavigateUrl = CSitemap.TranscriptChineseSimplified(Product)
            If IsChineseSimplified Then lnkSimp.CssClass = "selected"
            plhContentsPre.Controls.Add(lnkSimp)
        End If

        If Product.HasTranscriptChineseTrad Then
            Dim lnkTrad As New HyperLink
            lnkTrad.Text = "傳統"
            lnkTrad.Style.Add("float", "left")
            lnkTrad.NavigateUrl = CSitemap.TranscriptChineseTraditional(Product)
            If IsChineseTraditional Then lnkTrad.CssClass = "selected"
            plhContentsPre.Controls.Add(lnkTrad)
            lnkContents.Style.Add("float", "left")
        End If


        For Each i As CProduct In kids.Sermons
            If IsChineseSimplified Then
                If Not i.HasTranscriptChineseSimp Then Continue For
            ElseIf IsChineseTraditional Then
                If Not i.HasTranscriptChineseTrad Then Continue For
            Else
                If Not i.HasTranscript Then Continue For
            End If
            AddLink(i, kids.Sermons)
        Next
        If kids.Manuals.Count > 0 Then
            Dim m As CProduct = kids.Manuals(0)
            If m.Files.HasHtm Then
                Dim f As CFile = m.Files.FirstHtm

                Dim lnk As New HyperLink
                lnk.Text = "Manual&raquo;"
                lnk.Target = "_blank"
                lnk.NavigateUrl = CConfig.DropboxUrl(f.FileName)
                lnk.ToolTip = m.ProductName
                lnk.Style.Add("margin-left", "20px")
                plhContents.Controls.Add(lnk)
            End If
        End If
    End Sub
    Private Sub AddLink(i As CProduct, list As CProductList)
        Dim lnk As New HyperLink
        If i.ProductId = ProductId Then lnk.CssClass = "selected" Else lnk.CssClass = "menu"
        lnk.Text = 1 + list.IndexOf(i)
        If IsChineseSimplified Then
            lnk.NavigateUrl = CSitemap.TranscriptChineseSimplified(i)
        ElseIf IsChineseTraditional Then
            lnk.NavigateUrl = CSitemap.TranscriptChineseTraditional(i)
        Else
            lnk.NavigateUrl = CSitemap.TranscriptWithPaging(i)
        End If
        lnk.ToolTip = i.ProductName
        plhContents.Controls.Add(lnk)
    End Sub
    Private Function AddLink(plh As Control, i As CProduct, selectedId As Integer, lang As ELang) As HyperLink
        Dim lnk As New HyperLink
        plh.Controls.Add(lnk)

        lnk.Style.Add("display", "block")

        If i.HasParent Then lnk.Text = CUtilities.Truncate(i.ProductName, 27)


        Select Case lang
            Case ELang.English
                lnk.ToolTip = i.ProductName
                lnk.Text = CUtilities.Truncate(i.ProductName)
                lnk.NavigateUrl = CSitemap.TranscriptWithPaging(i)
            Case ELang.ChineseSimplified
                lnk.Text = CUtilities.Truncate(i.ProductNameChineseSimplified)
                lnk.ToolTip = i.ProductNameChineseSimplified & vbCrLf & i.ProductName
                lnk.NavigateUrl = CSitemap.TranscriptChineseSimplified(i)
            Case ELang.ChineseTraditional
                lnk.Text = CUtilities.Truncate(i.ProductNameChineseTraditionalOrSimplified)
                lnk.ToolTip = i.ProductNameChineseTraditionalOrSimplified & vbCrLf & i.ProductNameChineseTraditionalOrSimplified
                lnk.NavigateUrl = CSitemap.TranscriptChineseTraditional(i)
        End Select


        If i.ProductId = selectedId AndAlso (Me.Lang = lang) Then ' OrElse Me.Lang = ELang.ChineseTraditional AndAlso lang = ELang.ChineseSimplified) Then
            lnk.ForeColor = Drawing.Color.Black
            lnk.Font.Underline = True

            For Each j As CProduct In i.Children.Active.HavingTranscripts
                AddLink(plh, j, ProductId, lang).Style.Add("margin-left", "10px")
            Next
        End If
        Return lnk
    End Function
#End Region

End Class
